package com.cat.dynamicPlanning;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/longest-increasing-subsequence/
 * @create 2025/9/3 20:33
 * @since JDK17
 */

public class Solution62 {
    public int lengthOfLIS02(int[] nums) {
        int n = nums.length, ans = 1;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
            }
            ans = Math.max(ans, dp[i]);
        }

        return ans;
    }

    int f(int[] ends, int n, int target) {
        if (n == 0 || ends[n - 1] < target) {
            return n;
        }
        int l = -1, r = n, mid;
        while (l + 1 < r) {
            mid = (l + r) / 2;
            if (ends[mid] >= target) {
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
    public int lengthOfLIS(int[] nums) {
        int n = nums.length, cnt = 0;
        int[] ends = new int[n];
        for (int i = 0; i < n; i++) {
            int j = f(ends, cnt, nums[i]);
            if (j == cnt) {
                ends[cnt++] = nums[i];
            } else {
                ends[j] = nums[i];
            }
        }

        return cnt;
    }
}
